{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "75417ef5",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "from pandas import Series, DataFrame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "72f50802",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>tpep_pickup_datetime</th>\n",
       "      <th>passenger_count</th>\n",
       "      <th>trip_distance</th>\n",
       "      <th>total_amount</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2019-07-01 00:51:04</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.00</td>\n",
       "      <td>4.94</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2019-07-01 00:46:04</td>\n",
       "      <td>1.0</td>\n",
       "      <td>4.16</td>\n",
       "      <td>20.30</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2019-07-01 00:25:09</td>\n",
       "      <td>1.0</td>\n",
       "      <td>18.80</td>\n",
       "      <td>70.67</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2019-07-01 00:33:32</td>\n",
       "      <td>1.0</td>\n",
       "      <td>18.46</td>\n",
       "      <td>66.36</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2019-07-01 00:00:55</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.70</td>\n",
       "      <td>15.30</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  tpep_pickup_datetime  passenger_count  trip_distance  total_amount\n",
       "0  2019-07-01 00:51:04              1.0           0.00          4.94\n",
       "1  2019-07-01 00:46:04              1.0           4.16         20.30\n",
       "2  2019-07-01 00:25:09              1.0          18.80         70.67\n",
       "3  2019-07-01 00:33:32              1.0          18.46         66.36\n",
       "4  2019-07-01 00:00:55              0.0           1.70         15.30"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Load taxi data from January 2019 into a data frame, \n",
    "# using only the columns `tpep_pickup_datetime`, `passenger_count`, `trip_distance`, and `total_amount`, \n",
    "# making sure to load `tpep_pickup_datetime` as `datetime`.\n",
    "\n",
    "filename = '../data/nyc_taxi_2019-07.csv'\n",
    "\n",
    "df = pd.read_csv(filename,\n",
    "                usecols=['tpep_pickup_datetime', \n",
    "                         'trip_distance', \n",
    "                         'passenger_count', \n",
    "                         'total_amount'],\n",
    "                 parse_dates=['tpep_pickup_datetime'])\n",
    "\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "4c5c7133",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Export, to a tab-delimited CSV file, the columns tpep_pickup_datetime, passenger_count, trip_distance, total_amount\n",
    "# Use the format day/month/year HHs:MMm:SSs\n",
    "\n",
    "dt_format='%d/%m/%Y %Hh:%Mm:%Ss'\n",
    "\n",
    "df.to_csv('ex40_taxi_07_2019.csv',\n",
    "         sep='\\t',\n",
    "         columns=['tpep_pickup_datetime', \n",
    "                  'passenger_count', \n",
    "                  'trip_distance',\n",
    "                  'total_amount'],\n",
    "         date_format=dt_format)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "0a2f305b",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Read the CSV file that we just created into a data frame. \n",
    "# make sure to parse the datetime column (tpep_pickup_datetime) in its current format\n",
    "\n",
    "df = (\n",
    "    pd\n",
    "    .read_csv('ex40_taxi_07_2019.csv',\n",
    "              sep='\\t',\n",
    "              usecols=['tpep_pickup_datetime', \n",
    "                       'passenger_count', \n",
    "                       'trip_distance', \n",
    "                       'total_amount'],\n",
    "           parse_dates=['tpep_pickup_datetime'],\n",
    "           date_format=dt_format)\n",
    ")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "d2f10aeb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tpep_pickup_datetime    datetime64[ns]\n",
       "passenger_count                float64\n",
       "trip_distance                  float64\n",
       "total_amount                   float64\n",
       "dtype: object"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Check that the 'export_date' column is indeed a datetime\n",
    "df.dtypes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "adeefd8b",
   "metadata": {},
   "outputs": [],
   "source": [
    "all_dfs = pd.read_html('https://en.wikipedia.org/wiki/Apollo_program')\n",
    "df = all_dfs[2].copy()[['Date', 'Designation']]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "b4ce928e",
   "metadata": {},
   "outputs": [],
   "source": [
    "df['Date'] = pd.to_datetime(df['Date'].str.replace(\n",
    "        '(–.+)?,', '', regex=True))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "90e13224",
   "metadata": {},
   "outputs": [],
   "source": [
    "df = df.set_index('Date')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "fc5f444e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Designation</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Date</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1966-02-26</th>\n",
       "      <td>AS-201</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1966-07-05</th>\n",
       "      <td>AS-203</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1966-08-25</th>\n",
       "      <td>AS-202</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1967-02-21</th>\n",
       "      <td>AS-204 (Apollo 1)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1967-11-09</th>\n",
       "      <td>Apollo 4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1968-01-22</th>\n",
       "      <td>Apollo 5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1968-04-04</th>\n",
       "      <td>Apollo 6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1968-10-11</th>\n",
       "      <td>Apollo 7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1968-12-21</th>\n",
       "      <td>Apollo 8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1969-03-03</th>\n",
       "      <td>Apollo 9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1969-05-18</th>\n",
       "      <td>Apollo 10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1969-07-16</th>\n",
       "      <td>Apollo 11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1969-11-14</th>\n",
       "      <td>Apollo 12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1970-04-11</th>\n",
       "      <td>Apollo 13</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1971-01-31</th>\n",
       "      <td>Apollo 14</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1971-07-26</th>\n",
       "      <td>Apollo 15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1972-04-16</th>\n",
       "      <td>Apollo 16</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1972-12-07</th>\n",
       "      <td>Apollo 17</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                  Designation\n",
       "Date                         \n",
       "1966-02-26             AS-201\n",
       "1966-07-05             AS-203\n",
       "1966-08-25             AS-202\n",
       "1967-02-21  AS-204 (Apollo 1)\n",
       "1967-11-09           Apollo 4\n",
       "1968-01-22           Apollo 5\n",
       "1968-04-04           Apollo 6\n",
       "1968-10-11           Apollo 7\n",
       "1968-12-21           Apollo 8\n",
       "1969-03-03           Apollo 9\n",
       "1969-05-18          Apollo 10\n",
       "1969-07-16          Apollo 11\n",
       "1969-11-14          Apollo 12\n",
       "1970-04-11          Apollo 13\n",
       "1971-01-31          Apollo 14\n",
       "1971-07-26          Apollo 15\n",
       "1972-04-16          Apollo 16\n",
       "1972-12-07          Apollo 17"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "a6b005fa",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "18"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3a651598",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
